package HttpClient知识点;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.URI;


public class ApacheHttpGetDemo {
    public static void main(String[] args) throws Exception {
        // 1. 创建 HttpClient 实例
        CloseableHttpClient client = HttpClients.createDefault();

        // 2. 创建 HttpGet 请求对象，并设置 URL
        HttpGet get = new HttpGet("https://jsonplaceholder.typicode.com/posts/1");

        // 3. 执行请求，获取响应
        HttpResponse response = client.execute(get);

        // 4. 获取状态码
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("状态码: " + statusCode);

        // 5. 获取响应体内容
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        System.out.println("响应内容: \n" + responseBody);

        // 6. 关闭客户端连接
        client.close();
    }
}

